package com.example.batalhanaval;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	EditText edtLogin, edtSenha;
	TextView txtNLogin, txtSenha;
	Button btnOk, btnNovoReg, btnSair;
	RepositorioPlayer repositorioPlayer;
	Player player;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		player = new Player();
		repositorioPlayer = new RepositorioPlayer(this);

		btnNovoReg = (Button)findViewById(R.id.btnNovoReg);
		btnNovoReg.setOnClickListener(this);
		btnOk = (Button)findViewById(R.id.btnOkMain);
		btnOk.setOnClickListener(this);
		btnSair = (Button)findViewById(R.id.btnSair);
		btnSair.setOnClickListener(this);		

		edtLogin = (EditText)findViewById(R.id.editTextLogin);
		edtSenha = (EditText)findViewById(R.id.editTextSenha);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {

		String login = edtLogin.getText().toString();
		String senha = edtSenha.getText().toString();
		Intent it;


		switch (v.getId())
		{
		case R.id.btnNovoReg:
			it = new Intent(this, RegistroActivity.class);
			startActivity(it);
			break;

		case R.id.btnOkMain:

			if(login.equals(""))
			{
				Toast.makeText(this, R.string.msgLoginFieldEmpty, Toast.LENGTH_SHORT).show();
			}
			else if (senha.equals(""))
			{
				Toast.makeText(this, R.string.msgSenhaFieldEmpty, Toast.LENGTH_SHORT).show();
			}
			else
			{
				repositorioPlayer.processarLogin(login, senha);
				
				if(repositorioPlayer.processarLogin(login, senha) == null)
				{
					Toast.makeText(this, R.string.msgLoginFail, Toast.LENGTH_SHORT).show();
					onResume();
				}
				else
				{
				it = new Intent(this, HomeActivity.class);
				it.putExtra("player", repositorioPlayer.processarLogin(login, senha));
				startActivity(it);
				}
				

			}

			break;

		case R.id.btnSair:
			finish();
			break;

		}

	}
	
	@Override
	protected void onResume() {

	   super.onResume();
	   this.onCreate(null);
	}

}
